package com.zodiac.app.datathinking.szse.c0201;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.google.common.collect.Maps;
import com.zodiac.app.datathinking.szse.AbstractExtractor;
import lombok.Data;
import org.apache.commons.lang3.EnumUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Map;
import java.util.Objects;

@Component
public class C0201Extractor extends AbstractExtractor<C0201Extractor.Request, List> {
    private final RestTemplate restTemplate = new RestTemplate();

    @Override
    public List extract(Request request) {
        JSONArray response = restTemplate.getForObject(parseApiUrl(request.toParameterMap()), JSONArray.class);
        if (Objects.isNull(response)) {
            return null;
        }
        return (List) response.stream().map(item -> ((Map) item).get("data")).filter(item -> !((List) item).isEmpty()).findFirst().orElse(null);
    }

    public int count(Request request) {
        JSONArray response = restTemplate.getForObject(parseApiUrl(request.toParameterMap()), JSONArray.class);
        for (int i = 0; i < response.size(); i++) {
            if (!response.getJSONObject(i).getJSONArray("data").isEmpty()) {
                return response.getJSONObject(i).getJSONObject("metadata").getIntValue("recordcount");
            }
        }
        return 0;
    }

    @Data
    public static class Request {
        private String showtype = "JSON";
        private String catalogid = "1110x";
        private String tabkey;
        private int pageno = 1;

        public static Request create(Type type) {
            Request instance = new Request();
            instance.setTabkey(type.getValue());
            return instance;
        }

        public enum Type {
            ALL("上市公司列表", "tab1"),
            ZHU("主板", "tab2"),
            ZHONG("中小企业板", "tab3"),
            CHUANG("创业板", "tab4");
            private String name;
            private String key;
            private String value;

            Type(String name, String value) {
                this.name = name;
                this.key = this.name();
                this.value = value;
            }

            public String getName() {
                return name;
            }

            public String getKey() {
                return key;
            }

            public String getValue() {
                return value;
            }

            public static Type of(String value) {
                return EnumUtils.getEnumList(Type.class).stream().filter(item -> Objects.equals(item.getValue(), value)).findFirst().orElse(null);
            }
        }

        Map<String, Object> toParameterMap() {
            Map props = (Map) JSON.toJSON(this);
            Map<String, Object> parameterMap = Maps.newTreeMap();
            props.keySet().forEach(key -> parameterMap.put(key.toString().toUpperCase(), props.get(key)));
            return parameterMap;
        }
    }
}
